package com.tedu.show;

import java.awt.*;
import java.util.List;
import java.util.Map;

import javax.swing.JPanel;

import com.tedu.element.ElementObj;
import com.tedu.element.map.GameMap;
import com.tedu.manager.ElementManager;
import com.tedu.manager.GameElement;

/**
 * @说明 游戏的主要面板
 * @功能说明 主要进行元素的显示，同时进行界面的刷新(多线程)
 */
public class GameMainJPanel extends JPanel implements Runnable {
    //	联动管理器
    private ElementManager em;

    public GameMainJPanel() {
        init();
    }

    public void init() {
        em = ElementManager.getManager();
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);

        Map<GameElement, List<ElementObj>> all = em.getGameElements();
        GameMap gameMap = em.getGameMap();

        if (gameMap == null) {
            // 如果未加载，显示加载中提示，并直接返回
            g.setColor(Color.BLACK);
            g.fillRect(0, 0, getWidth(), getHeight());
            g.setColor(Color.WHITE);
            g.setFont(new Font("宋体", Font.BOLD, 24));
            g.drawString("Loading...", getWidth() / 2 - 50, getHeight() / 2);
            return;
        }

        gameMap.showMapFloor(g);

        for (GameElement ge : GameElement.values()) {
            List<ElementObj> list = all.get(ge);
            for (int i = 0; i < list.size(); i++) {
                ElementObj obj = list.get(i);
                obj.showElement(g);
            }
        }
        gameMap.showMapGrid(g);
    }

    @Override
    public void run() {
        while (true) {
            this.repaint();

            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                break;
            }
        }
    }
}
